package com.aplana.ccsd.service;

import info.magnolia.cms.core.MgnlNodeType;
import info.magnolia.cms.util.ContentUtil;
import info.magnolia.jcr.util.NodeUtil;
import info.magnolia.jcr.util.PropertyUtil;
import info.magnolia.module.blossom.dialog.TabBuilder;
import info.magnolia.module.templatingkit.dam.Asset;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.jcr.Node;
import javax.jcr.RepositoryException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.aplana.ccsd.model.Article;
import com.aplana.ccsd.model.ArticleHeader;
import com.aplana.ccsd.model.RecentArticles;
import com.aplana.ccsd.util.JcrNodeUtils;

@SuppressWarnings("deprecation")
@Service
public class ArticleServiceImpl implements ArticleService {

	@Autowired
	private TemplatingService templatingService;

	@Override
	public ArticleHeader getArticleHeader(Node articlePage) {
		String headline = PropertyUtil.getString(articlePage, "title");

		Calendar calendar = PropertyUtil.getDate(articlePage, "date");
		String author = PropertyUtil.getString(articlePage, "author");
		String abstractText = PropertyUtil.getString(articlePage, "abstract");
		Asset image = templatingService.getNodePropertyAsImage(articlePage,
				"image");

		ArticleHeader articleHeader = new ArticleHeader(headline,
				calendar == null ? null : calendar.getTime(), author,
				abstractText, image);

		return articleHeader;
	}

	@Override
	public List<Article> findArticles(Node articlesParentPage)
			throws RepositoryException {
		// javax.jcr.query.Query q;
		Iterable<Node> pages = NodeUtil.getNodes(articlesParentPage,
				MgnlNodeType.NT_PAGE);
		List<Article> articles = new ArrayList<Article>();
		for (Node page : pages) {
			if (!ContentUtil.asContent(page).hasMixin(MgnlNodeType.MIX_DELETED)) {
				ArticleHeader header = getArticleHeader(page);
				Article article = new Article(header, JcrNodeUtils.getURL(page));
				articles.add(article);
			}
		}
		return articles;
	}

	@Override
	public void addArticleListDialogControls(TabBuilder tabBuilder) {
		tabBuilder.addEdit("title", "Title", "");
		tabBuilder.addLink("articlesLink", "Страница со новостями",
				"Ссылка на страницу содержащую страницы со статьями");
	}
	
	@Override
	public void addRecentArticlesOverviewDialogControls(TabBuilder tabBuilder) {
		tabBuilder.addEdit("title", "Заголовок", "");
		tabBuilder.addEdit("allArticlesLinkTitle", "Наименование ссылки 'Все'", "");
		tabBuilder.addEdit("showArticlesNumber", "Число показываемых статей", "");
		tabBuilder.addLink("articlesLink", "Страница со статьями",
				"Ссылка на страницу содержащую страницы со статьями");
	}

	@Override
	public Node getArticleListPage(Node articlesComponent)
			throws RepositoryException {
		String articlesNodeLink = PropertyUtil.getString(articlesComponent,
				"articlesLink");

		Node articlesPage = articlesComponent.getSession().getNode(
				articlesNodeLink);
		return articlesPage;
	}

	@Override
	public RecentArticles getRecentArticles(Node recentArticlesComponent)
			throws RepositoryException {
		Node articleListPage = getArticleListPage(recentArticlesComponent);
		List<Article> allResults = findArticles(articleListPage);
		
		String showNumber = PropertyUtil.getString(recentArticlesComponent, "showArticlesNumber");
		int showNum = 0;
		List<Article> recentResults = allResults;
		if (!org.apache.commons.lang.StringUtils.isBlank(showNumber)) {
			showNum = Integer.decode(showNumber);
			
			if (allResults.size()>showNum) {
				recentResults = allResults.subList(0, showNum);
			}
		}
		
		String title = PropertyUtil.getString(recentArticlesComponent, "title");
		String allArticlesLinkTitle = PropertyUtil.getString(recentArticlesComponent, "allArticlesLinkTitle");
		return new RecentArticles(recentResults, JcrNodeUtils.getURL(articleListPage), title, allArticlesLinkTitle);
	}

}